package com.qdsgjsfk.vision.base;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import com.qdsgjsfk.vision.R;

import java.util.Objects;

import butterknife.ButterKnife;
import util.NetworkUtil;

/**
 * 这个项目没有viewpager  这个类用不到
 */

public abstract class BaseFragment extends Fragment {

    protected View rootView;

    //当前Fragment是否处于可见状态标志，防止因ViewPager的缓存机制而导致回调函数的触发
    private boolean isFragmentVisible;
    //是否是第一次开启网络加载
    public boolean isFirst;
    protected Activity mContext;
    private static ProgressDialog progressDialog;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (rootView == null)
            rootView = inflater.inflate( getLayoutResource(), container, false );
        ButterKnife.bind( this, rootView );
        initData();
        initView();
        //可见，但是并没有加载过
        if (isFragmentVisible && !isFirst) {
            onFragmentVisibleChange( true );
        }
        return rootView;
    }

    //获取布局文件
    protected abstract int getLayoutResource();


    //初始化view
    protected abstract void initView();

    protected abstract void initData();

    /**
     * 通过Class跳转界面
     **/
    public void startActivity(Class<?> cls) {
        startActivity( cls, null );
    }

    /**
     * 通过Class跳转界面
     **/
    public void startActivityForResult(Class<?> cls, int requestCode) {
        startActivityForResult( cls, null, requestCode );
    }

    /**
     * 含有Bundle通过Class跳转界面
     **/
    public void startActivityForResult(Class<?> cls, Bundle bundle,
                                       int requestCode) {
        Intent intent = new Intent();
        intent.setClass( getActivity(), cls );
        if (bundle != null) {
            intent.putExtras( bundle );
        }
        startActivityForResult( intent, requestCode );
    }

    /**
     * 含有Bundle通过Class跳转界面
     **/
    public void startActivity(Class<?> cls, Bundle bundle) {
        Intent intent = new Intent();
        intent.setClass( Objects.requireNonNull( getActivity() ), cls );
        if (bundle != null) {
            intent.putExtras( bundle );
        }
        startActivity( intent );
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint( isVisibleToUser );
        if (isVisibleToUser) {
            isFragmentVisible = true;
        }
        if (rootView == null) {
            return;
        }
        //可见，并且没有加载过
        if (!isFirst && isFragmentVisible) {
            onFragmentVisibleChange( true );
            return;
        }
        //由可见——>不可见 已经加载过
        if (isFragmentVisible) {
            onFragmentVisibleChange( false );
            isFragmentVisible = false;
        }
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    /**
     * 当前fragment可见状态发生变化时会回调该方法
     * 如果当前fragment是第一次加载，等待onCreateView后才会回调该方法，其它情况回调时机跟 {@link #setUserVisibleHint(boolean)}一致
     * 在该回调方法中你可以做一些加载数据操作，甚至是控件的操作.
     *
     * @param isVisible true  不可见 -> 可见
     *                  false 可见  -> 不可见
     */
    protected void onFragmentVisibleChange(boolean isVisible) {

    }

    @Override
    public void onAttach(Context context) {
        mContext = (Activity) context;
        super.onAttach( context );
    }

    /**
     * 展示加载框
     *
     * @time 2020/4/21 11:16
     */
    public void showProgressDialog() {
        if (NetworkUtil.isNetworkAvailable( getActivity() )) {
            // 这个函数用来设置 Dialog 周围的颜色。系统默认的是半透明的灰色。值设为0则为完全透明。
            Objects.requireNonNull( getActivity() ).getWindow().setDimAmount( 3 );
            progressDialog = new ProgressDialog( getActivity() );
            //设置进度条风格，风格为圆形，旋转的
            progressDialog.setProgressStyle(
                    ProgressDialog.STYLE_SPINNER );
            //设置ProgressDialog 标题
//        progressDialog.setTitle("下载");
            //设置ProgressDialog 提示信息
            progressDialog.setMessage( "加载中..." );
            //设置ProgressDialog 标题图标
//        progressDialog.setIcon(android.R.drawable.btn_star);
            //设置ProgressDialog 的进度条是否不明确
            progressDialog.setIndeterminate( false );
            //设置ProgressDialog 是否可以按退回按键取消
            progressDialog.setCancelable( true );
            progressDialog.setProgressStyle( R.style.loading_dialog );
            // 让ProgressDialog显示
            progressDialog.show();
        }

    }

    public void dismissProgressDialog() {
//        mHandler.postDelayed( sRunnable, 1000 );
        progressDialog.dismiss();
    }

    public void hintKeyBoard(EditText odSph) {
        //拿到InputMethodManager
        InputMethodManager imm = (InputMethodManager) mContext.getSystemService( Context.INPUT_METHOD_SERVICE );
        assert imm != null;
        imm.hideSoftInputFromWindow( odSph.getWindowToken(), 0 );
        //如果window上view获取焦点 && view不为空
//        if (imm.isActive() && activity.getCurrentFocus() != null) {
//            //拿到view的token 不为空
//            if (activity.getCurrentFocus().getWindowToken() != null) {
//                //表示软键盘窗口总是隐藏，除非开始时以SHOW_FORCED显示。
//                imm.hideSoftInputFromWindow( activity.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS );
//            }
//        }
    }


    /**
     * EditText获取焦点并显示软键盘
     */
    public void requestFocusKeyBoard(EditText editText) {
        editText.setInputType( EditorInfo.TYPE_CLASS_PHONE );
        editText.setFocusable( true );
        editText.setFocusableInTouchMode( true );
        editText.requestFocus();
        InputMethodManager imm = (InputMethodManager) Objects.requireNonNull( getActivity() ).getSystemService( Context.INPUT_METHOD_SERVICE );
        assert imm != null;
        imm.toggleSoftInput( InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_NOT_ALWAYS );
    }

    /**
     * EditText获取焦点并显示软键盘
     */
    public void requestFocus(EditText editText) {
        editText.setFocusable( true );
        editText.setFocusableInTouchMode( true );
        editText.requestFocus();
        InputMethodManager imm = (InputMethodManager) mContext.getSystemService( Context.INPUT_METHOD_SERVICE );
        assert imm != null;
        imm.toggleSoftInput( InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_NOT_ALWAYS );
    }


}